import { postAllContries } from "./../../api/api";
import { defineStore } from "pinia";
import { ref, computed } from "vue";
import { CountryListItem } from "../../types/global";
import { useGlobalStore } from "./global";
export const useCountryStore = defineStore("countryStore", () => {
  const globalStore = useGlobalStore();

  const country = ref<undefined | any[]>();
  const languageIndexCache = ref(-1);
  const countrySelectOptions = ref();
  const setCountry = (val: any) => {
    country.value = val;
    countrySelectOptions.value = country.value?.map((item) => {
      return {
        ...item,
        value: item.id,
        label: item.title,
      };
    })!;
  };

  const clearCountry = () => {
    country.value = undefined;
  };
  const getCountryData = () => {
    
    setCountry(globalStore.nationalData);
  };

  const getCountryById = (id: string) => {
    let countryName = "";
    country.value?.map((item) => {
      if (item.id == parseInt(id)) {
        countryName = item.title;
      }
    });
    return countryName;
  };

  const getCountryIdByTitle = (title: string) => {
    let id = "";
    country.value?.map((item) => {
      if (item.title == title) {
        id = item.id + "";
      }
    });
    return id;
  };

  return {
    country,
    languageIndexCache,
    countrySelectOptions,
    getCountryData,
    getCountryById,
    getCountryIdByTitle,
    setCountry,
    clearCountry,
  };
});
